<template>
  <el-dialog :close-on-click-modal='false' :title="$t('查看')" v-drag :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" @close='close'>
    <el-form ref="form" :model="record" label-width="100px" :rules="rules">
      <el-form-item :label="$t('隧道名称')" prop="TunnelName">
        {{record.TunnelName}}
      </el-form-item>
      <el-form-item :label="$t('共享秘钥')" prop="PreSharedKey">
        {{record.PreSharedKey}}
      </el-form-item>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane :label="$t('本端配置')" name="local" size="small">
          <el-form-item :label="$t('本端IP')" prop="LocalIp">
            {{record.LocalIp}}
          </el-form-item>
          <div>
            <el-table size='mini' :data="record.LocalIpv4Net" style="width: 100%">
              <el-table-column prop='Ipv4Net' align='center' :label="$t('子网')">
                <template slot-scope="scope">
                  <tooltip :copyText="scope.row.Ipv4Net" class="wid190">
                  </tooltip>
                </template>
              </el-table-column>
              <el-table-column prop='Netmask' align='center' :label="record.IpType === 'v4'?$t('子网掩码') : $t('前缀长度')">
                <template slot-scope="scope">
                  <tooltip :copyText="scope.row.Netmask" class="wid190">
                  </tooltip>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="$t('远端配置')" name="remote" size="small">
          <el-form-item :label="$t('远端IP')" prop="RemoteIp">
            {{record.RemoteIp}}
          </el-form-item>
          <div>
            <el-table size='mini' :data="record.RemoteIpv4Net" style="width: 100%">
              <el-table-column prop='Ipv4Net' align='center' :label="$t('子网')">
                <template slot-scope="scope">
                  <tooltip :copyText="scope.row.Ipv4Net" class="wid190">
                  </tooltip>
                </template>
              </el-table-column>
              <el-table-column prop='Netmask' align='center' :label="record.IpType === 'v4'?$t('子网掩码') : $t('前缀长度')">
                <template slot-scope="scope">
                  <tooltip :copyText="scope.row.Netmask" class="wid190">
                  </tooltip>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  name: 'ipsecvpnCheck',
  data () {
    return {
      activeName: 'local',
      dialogVisible: false,
      record: {
        Index: null,
        IpType: 'v4',
        LocalIp: '',
        LocalIpv4Net: [],
        PreSharedKey: '',
        RemoteIp: '',
        RemoteIpv4Net: [],
        TunnelName: '',
        TunnelOpenOrClose: '',
        TunnelStatus: ''
      },
      recordCopy: {
        Index: null,
        IpType: 'v4',
        LocalIp: '',
        LocalIpv4Net: [],
        PreSharedKey: '',
        RemoteIp: '',
        RemoteIpv4Net: [],
        TunnelName: '',
        TunnelOpenOrClose: '',
        TunnelStatus: ''
      },
      rules: {}
    }
  },
  methods: {
    handleClick (tab, event) {
      this.activeName = tab.name
    },
    init (data) {
      this.dialogVisible = true
      if (data) {
        this.record = Object.assign(this.record, data)
        if (this.isValidIP(this.record.LocalIp)) {
          this.record.IpType = 'v4'
        } else if (this.isValidIPv6(this.record.LocalIp)) {
          this.record.IpType = 'v6'
        }
        data.LocalIpNet.forEach(item => {
          this.record.LocalIpv4Net.push({ Ipv4Net: item.IpNet, Netmask: item.Netmask, lipIndex: item.lipIndex })
        })
        data.RemoteIpNet.forEach(item => {
          this.record.RemoteIpv4Net.push({ Ipv4Net: item.IpNet, Netmask: item.Netmask, ripIndex: item.ripIndex })
        })
      }
    },
    close () {
      this.record = Object.assign({}, this.recordCopy)
      this.record.LocalIpv4Net = []
      this.record.RemoteIpv4Net = []
      this.old_LocalIp = []
      this.old_RemoteIp = []
      this.dialogVisible = false
    },
    // 验证ipv4
    isValidIP (ip) {
      let reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      return reg.test(ip)
    },
    // 验证ipv6
    isValidIPv6 (ip) {
      let reg = /^\s*((([0-9A-Fa-f]{1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:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?\s*$/
      return reg.test(ip)
    }
  }
}
</script>
